﻿using System.Collections;

using UnityEngine;
using UnityEngine.UI;
//继承MovingObject类
public class Player : MovingObject {

    public int wallDamage = 1; //对墙壁的伤害
    public int pointsPerFood = 10; //食物的回复量
    public int pointsPerSoda = 20; //苏打水的回复量
    public float restartlevelDelay = 1f; //进入下一关的时间差

    public Text foodText; //FoodText

    //指定各音效
    public AudioClip moveSound1;
    public AudioClip moveSound2;
    public AudioClip eatSound1;
    public AudioClip eatSound2;
    public AudioClip drinkSound1;
    public AudioClip drinkSound2;
    public AudioClip gameOverSound;

    private Animator animator; //PlayerChop, PlayerHit用
    private int food; //Player体力

    //********** 修改开始 **********//
    //触摸点的初始坐标 Vector2(-1.0, -1.0)
    private Vector2 touchOrigin = -Vector2.one;
    //********* 修改结束 **********//

    //继承MovingObject的Start方法　用base调用
    protected override void Start ( ) {
        //获取Animator
        animator = GetComponent<Animator> ( );
        //从单例的GameManager中获取playerFoodPoints
        //即使跨越场景（关卡）也能保持变量
        food = GameManager.instance.playerFoodPoints;

        foodText.text = "Food: " + food;

        //调用MovingObject的Start
        base.Start ( );
    }
    //Player脚本无效之前、体力保存至GameManager
    //Unity的生命周期API方法
    private void OnDisable ( ) {
        GameManager.instance.playerFoodPoints = food;
    }

    void Update ( ) {
        //Player回合之外Update的处理直接跳出
        if (!GameManager.instance.playersTurn)
            return;

        int horizontal = 0;
        int vertical = 0;

        //********** 修改开始 **********//

        //用于判断平台
        //键盘操作
#if UNITY_STANDALONE || UNITY_WEBPLAYER
        horizontal = (int) Input.GetAxisRaw ("Horizontal");
        vertical = (int) Input.GetAxisRaw ("Vertical");

        if (horizontal != 0) {
            vertical = 0;
        }
        //触摸操作
#else
        //触摸的位置超过0处，也就是发生了触摸
        if (Input.touchCount > 0) {
            //touches数组中、选择使用最初的那组
            Touch myTouch = Input.touches [0];
            //如果开始触摸
            if (myTouch.phase == TouchPhase.Began) {
                //使touchOrigin获取触摸的坐标
                touchOrigin = myTouch.position;
            }
            //如果手指从画面中离开，并且横方向大于0(防止重复判定)
            else if (myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0) {
                //获取手指离开时的位置
                Vector2 touchEnd = myTouch.position;
                //计算从touchEnd.x到touchOrigin.x延伸出来横方向的移动长度
                float x = touchEnd.x - touchOrigin.x;
                //计算从touchEnd.y到touchOrigin.y延伸出来纵方向的移动长度
                float y = touchEnd.y - touchOrigin.y;
                //防止再次进入if文 强制修改touchOrigin.x
                touchOrigin.x = -1;
                //横轴的移动长度大于纵轴的移动长度时，设定成horizontal(横)
                if (Mathf.Abs (x) > Mathf.Abs (y))
                    //x笔0大的时候判定为(右移动)、小的话判定为-1(左移动)
                    horizontal = x > 0 ? 1 : -1;
                //纵轴的移动长度大于横轴的移动长度时，设定成vertical(纵)
                else
                    //y比0大的时候判定为1(上移动)、小的时候判定为-1(下移动)
                    vertical = y > 0 ? 1 : -1;
            }
        }
        //平台判定结束
#endif

        //********* 修改结束 **********//
        //无论进行上下左右移动之时
        if (horizontal != 0 || vertical != 0) {
            //Wall: 指定传入范型<T>类
            //Player的场合只需要判定Wall
            AttemptMove<Wall> (horizontal, vertical);
        }
    }

    protected override void AttemptMove<T> (int xDir, int yDir) {
        //移动1回减少1个食物
        food--;

        foodText.text = "Food: " + food;

        //调用MovingObject的AttemptMove
        base.AttemptMove<T> (xDir, yDir);

        RaycastHit2D hit;

        //能够移动时moveSound1或moveSound2随机播放
        if (Move (xDir, yDir, out hit)) {
            SoundManager.instance.RandomizeSfx (moveSound1, moveSound2);
        }

        CheckIfGameOver ( );
        //Player回合结束
        GameManager.instance.playersTurn = false;
    }

    //必须复写MovingObject的抽象方法
    protected override void OnCantMove<T> (T component) {
        //用Wall型用来反射获取 Wall脚本
        Wall hitWall = component as Wall;
        //调用Wall脚本的DamageWall方法
        hitWall.DamageWall (wallDamage);
        //触发对Wall攻击的动画
        animator.SetTrigger ("PlayerChop");
    }

    private void OnTriggerEnter2D (Collider2D other) {
        if (other.tag == "Exit") {
            //Invoke: 根据参数延迟执行方法
            Invoke ("Restart", restartlevelDelay);
            enabled = false; //使Player脚本无效(禁止操作)
        } else if (other.tag == "Food") {
            //体力回复删除other对象
            food += pointsPerFood;
            foodText.text = "+" + pointsPerFood + " Food: " + food;

            //获取Food时候、eatSound1或者eatSound2随机播放
            SoundManager.instance.RandomizeSfx (eatSound1, eatSound2);

            other.gameObject.SetActive (false);
        } else if (other.tag == "Soda") {
            //体力回复删除other对象
            food += pointsPerSoda;

            foodText.text = "+" + pointsPerSoda + " Food: " + food;

            //获取Soda时候、播放drinkSound1或者drinkSound2
            SoundManager.instance.RandomizeSfx (drinkSound1, drinkSound2);

            other.gameObject.SetActive (false);
        }
    }

    private void Restart ( ) {
        //读取相同的场景
        Application.LoadLevel (Application.loadedLevel);
    }
    //敌方角色对Player攻击时执行的方法
    public void LoseFood (int loss) {
        animator.SetTrigger ("PlayerHit");
        food -= loss;

        foodText.text = "-" + loss + " Food: " + food;

        CheckIfGameOver ( );
    }

    private void CheckIfGameOver ( ) {
        if (food <= 0) {

            //播放gameOverSound
            SoundManager.instance.PlaySingle (gameOverSound);
            //停止BGM
            SoundManager.instance.musicSource.Stop ( );

            //执行GameManager的GameOver方法 
            GameManager.instance.GameOver ( );
        }
    }
}
